//
//  FTYShoppingCartPort.m
//  RedWine
//
//  Created by Liufangfang on 2017/1/4.
//  Copyright © 2017年 HuiBangKe. All rights reserved.
//

#import "FTYShoppingCartPort.h"
#import "FTYNavigationController.h"
#import "RWLoginViewController.h"
#import "ShopCartModel.h"

#define kAddCartSubUrl @"/shop/ybkapi.php?act=ybkapi&op=newcart"
#define kUpdateCartSubUrl @"/shop/ybkapi.php?act=ybkapi&op=updatecart"

@implementation FTYShoppingCartPort

+ (void)requestShoppingCartWithProjectID:(NSString *)projectID viewController:(UIViewController *)vc block:(ReturnStateBlock)block{
    if ([FTYUser sharedUser].userID.length > 0) {
        
        FTYUser *user = [FTYUser sharedUser];
       
        NSString *url = [NSString stringWithFormat:@"%@&goods_id=%@&goods_num=%@&member_id=%@&token=%@", kAddCartSubUrl, projectID, @(1), user.userID, user.userToken];
        
        [FTYHTTPManager getInfoWithSubUrl:url parameters:nil block:^(id result, NSError *error) {
            if (!error) {
                
                DLog(@"加入购物车====%@", result);
                FTYBaseShopCartModel *baseModel = [[FTYBaseShopCartModel alloc] init];
                baseModel = [FTYBaseShopCartModel mj_objectWithKeyValues:result];
                FTYErrorInfoModel *errorInfo = [FTYErrorInfoModel mj_objectWithKeyValues:baseModel.err];
                FTYCartInfoModel *cartInfo = [FTYCartInfoModel mj_objectWithKeyValues:baseModel.info];
                if (0 == errorInfo.errorcode) {
                    [[FTYToastView alloc] addTitle:@"加入购物车成功" addView:vc.view];
                    ShopCartModel *shopCart = [[ShopCartModel alloc] init];
                    shopCart.goods_id = [projectID integerValue];
                    shopCart.goods_num = 1;
                    shopCart.cart_id = cartInfo.cart_id;
                    [user writeUserShopCart:shopCart];
                    
                    [[NSNotificationCenter defaultCenter] postNotificationName:@"AddShopCartNumberNotification" object:nil];
                } else {
                    [[FTYToastView alloc] addTitle:errorInfo.errorinfo addView:vc.view];
                }
            } else {
                
            }
        }];
        block(YES);
    }else{
//        [[self class] loginAction:vc];
        [FTYLoginManager reLoginFromViewController:vc];
    }
}

+ (void)updateShoppingCartWithShopCartID:(NSString *)shopCartID goodsNum:(NSInteger)goodsNum viewController:(UIViewController *)vc block:(ReturnStateBlock)block{
    if ([FTYUser sharedUser].userID.length > 0) {
        FTYUser *user = [FTYUser sharedUser];
       
            NSString *goods_num = [NSString stringWithFormat:@"%ld",goodsNum];
            NSString *url = [NSString stringWithFormat:@"%@&cart_id=%@&goods_num=%@&member_id=%@&token=%@", kUpdateCartSubUrl, shopCartID, goods_num, user.userID, user.userToken];
        
        
        [FTYHTTPManager getInfoWithSubUrl:url parameters:nil block:^(id result, NSError *error) {
            if (!error) {
                
                DLog(@"更新购物车====%@", result);
                FTYBaseModel *baseModel = [[FTYBaseModel alloc] init];
                baseModel = [FTYBaseModel mj_objectWithKeyValues:result];
                FTYErrorInfoModel *errorInfo = [FTYErrorInfoModel mj_objectWithKeyValues:baseModel.err];
                
                if (0 == errorInfo.errorcode) {
                    [[FTYToastView alloc] addTitle:@"更新购物车成功" addView:vc.view];
                } else {
                    [[FTYToastView alloc] addTitle:errorInfo.errorinfo addView:vc.view];
                }
            } else {
                
            }
        }];
        block(YES);
    }else{
        //        [[self class] loginAction:vc];
        [FTYLoginManager reLoginFromViewController:vc];
    }
}

+ (void)addShoppingCartWithProjectID:(NSString *)projectID viewController:(UIViewController *)vc block:(ReturnStateBlock)block
{
    ShopCartModel *shopCartModel = [FTYShoppingCartPort getShopCartByGoodsId:projectID];
    if (!shopCartModel) {
        [FTYShoppingCartPort requestShoppingCartWithProjectID:projectID viewController:vc block:block];
    }else{
        NSString *shopCartId = [NSString stringWithFormat:@"%ld",shopCartModel.cart_id];
        [FTYShoppingCartPort updateShoppingCartWithShopCartID:shopCartId goodsNum:shopCartModel.goods_num viewController:vc block:block];
    }
}


+ (ShopCartModel *)getShopCartByGoodsId:(NSString *)goodsId
{
    /*
    ShopCartModel *shopCartModel = [[FTYUser sharedUser] userShopCartByGoodsId:goodsId];
    if (shopCartModel) {
        return shopCartModel;
    }
    */
    NSArray *shopCartArr = [FTYShoppingCartPort getShopCartList];
    for (ShopCartModel *model in shopCartArr) {
        if (model.goods_id == [goodsId integerValue]) {
            return model;
        }
    }
    
    return nil;
}

- (void)doGoodId:(NSString *)goodsId
{
    NSArray *shopCartArr = [FTYShoppingCartPort getShopCartList];
}


+ (NSArray *)getShopCartList
{
    FTYUser *user = [FTYUser sharedUser];
    __block NSArray *dataArr;
    [FTYHTTPManager postInfoWithSubUrl:[NSString stringWithFormat:@"/shop/ybkapi.php?act=ybkapi&op=cartlist&member_id=%@&token=%@", user.userID, user.userToken] parameters:nil block:^(id result, NSError *error) {
        
        if (!error) {
            
            DLog(@"购物车====%@", result);
            FTYBaseModel *baseModel = [[FTYBaseModel alloc] init];
            baseModel = [FTYBaseModel mj_objectWithKeyValues:result];
            FTYErrorInfoModel *errorInfo = [FTYErrorInfoModel mj_objectWithKeyValues:baseModel.err];
            
            if (0 == errorInfo.errorcode) {
                
                NSMutableArray *array = [NSMutableArray array];
                [array addObjectsFromArray:baseModel.info];
                //字典数组 ==> 模型数组
                dataArr = [ShopCartModel mj_objectArrayWithKeyValuesArray:array];
                
                
                DLog(@"购物车列表=====%@", dataArr);
                
            } else {
                
            }
        } else {
            DLog(@"购物车获取信息失败：%@", result);
        }
    }];
    
    return dataArr;
}

+ (void)loginAction:(UIViewController *)vc{
    
    RWLoginViewController *loginVC = [[RWLoginViewController alloc] init];
    FTYNavigationController *loginNC = [[FTYNavigationController alloc] initWithRootViewController:loginVC];
    loginNC.navigationBar.translucent = NO;
    loginNC.navigationBar.barTintColor = [FTYColor getMainColorWithAlpha:1.];
    loginNC.navigationBar.tintColor = [UIColor whiteColor];
    [loginNC.navigationBar setTitleTextAttributes:@{NSFontAttributeName: FTY_FONTTSIZE(20),
                                                    NSForegroundColorAttributeName: [UIColor whiteColor]}];
    [vc presentViewController:loginNC animated:YES completion:nil];
}

@end
